"""
Case Type   : 资源池化集群参数同步
Case Name   : 主机执行gs_guc reload，备机参数同步
Create At   : 2025/05/30
Owner       : @congzhou2603
Description :
    1.主机配置sighub级别参数incremental_checkpoint_timeout和postmaster级别参数max_connections
    2.备机查询"incremental_checkpoint_timeout"和"max_connections"参数
    3.重启集群
    4.备机查询"incremental_checkpoint_timeout"和"max_connections"参数
Expect      :
    1.成功
    2.备机max_connections不生效，incremental_checkpoint_timeout生效
    3.成功
    4.备机max_connections生效，incremental_checkpoint_timeout生效
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


class ParamSync(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.st1_sh = CommonSH('Standby1DbUser')
        self.st2_sh = CommonSH('Standby2DbUser')
        self.pri_root_node = Node(node="PrimaryRoot")
        self.pri_user_node = Node(node="PrimaryDbUser")
        self.st1_user_node = Node(node="Standby1DbUser")
        self.st2_user_node = Node(node="Standby2DbUser")
        self.default_incremental_checkpoint_timeout = \
            self.com.show_param("incremental_checkpoint_timeout")
        self.default_max_connections = \
            self.com.show_param("max_connections")


    def test_hashmap(self):
        step = '--step1:主机配置sighub级别参数和postmaster参数 expect:成功--'
        self.log.info(step)

        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG, "incremental_checkpoint_timeout = 1234", single = True)
        self.log.info(guc_res)
        self.assertTrue(guc_res, "执行失败" + step)

        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG, "max_connections = 2345", single = True)
        self.log.info(guc_res)
        self.assertTrue(guc_res, "执行失败" + step)
        time.sleep(10)

        step = '--step2:备机查询相关参数参数 expect:备机max_connections不生效，incremental_checkpoint_timeout生效--'
        self.log.info(step)
        standby_incremental_checkpoint_timeout = self.st1_sh.execut_db_sql('''show incremental_checkpoint_timeout;''')
        self.log.info("standby incremental_checkpoint_timeout:" + standby_incremental_checkpoint_timeout)
        self.assertEqual("1234s", standby_incremental_checkpoint_timeout.split("\n")[-2].strip())
        standby_max_connections = self.st1_sh.execut_db_sql('''show max_connections;''')
        self.log.info("standby max_connections:" + standby_max_connections)
        self.assertEqual(self.default_max_connections, standby_max_connections.split("\n")[-2].strip())

        step = '--step3:重启集群 expect:成功--'
        restart_res = self.primary_sh.restart_db_cluster()
        self.log.info(restart_res)
        self.assertTrue(restart_res, "执行失败" + step)

        step = '--step4:备机查询"incremental_checkpoint_timeout"和"max_connections"参数 expect:生效--'
        self.log.info(step)
        standby_incremental_checkpoint_timeout = self.st1_sh.execut_db_sql('''show incremental_checkpoint_timeout;''')
        self.log.info("standby incremental_checkpoint_timeout:" + standby_incremental_checkpoint_timeout)
        self.assertEqual("1234s", standby_incremental_checkpoint_timeout.split("\n")[-2].strip())
        standby_max_connections = self.st1_sh.execut_db_sql('''show max_connections;''')
        self.log.info("standby max_connections:" + standby_max_connections)
        self.assertEqual("2345", standby_max_connections.split("\n")[-2].strip())

    def tearDown(self):
        self.log.info('----恢复环境----')
        switch_res = self.primary_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(switch_res)

        pararm_sync_par = [
            f"incremental_checkpoint_timeout={self.default_incremental_checkpoint_timeout}",
            f"max_connections={self.default_max_connections}",
        ]
        for par in pararm_sync_par:
            guc_res = self.primary_sh.execute_gsguc(
                "set", self.constant.GSGUC_SUCCESS_MSG, par)
            self.log.info(guc_res)
            self.assertTrue(guc_res, "恢复环境失败")
        restart_res = self.primary_sh.restart_db_cluster()
        self.log.info(restart_res)
        self.assertIn(self.constant.cm_switchover_success_msg, switch_res,
                      '恢复环境失败')
        self.assertTrue(restart_res, "恢复环境失败")
        self.log.info(f'----{os.path.basename(__file__)} end')
